import { queryKeys, addKeys,removeKeys,editKeys,updateKey,queryHistoryKey,querySelectKeyRule,querySelectApp,querySelectScene} from '@/services/security/keyMngt';

export default {
  namespace: 'keyMngt',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    historyData: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    keyRuleData:{},
    appData:{},
    sceneData:{},
    queryForm:{},
  },
  effects: {
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryKeys, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addKeys, payload);
      yield put({
        type: 'saveadd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeKeys, payload);
      yield put({
        type: 'saveremove',
        payload: response,
      });
      if (callback) callback(response);
    },
    // *update({ payload, callback }, { call, put }) {
    //   const response = yield call(editKeys, payload);
    //   yield put({
    //     type: 'saveupdate',
    //     payload: response,
    //   });
    //   if (callback) callback(response);
    // }, 
    
    *updateKey({ payload, callback }, { call, put }) {
      const response = yield call(updateKey, payload);
      yield put({
        type: 'saveupdate',
        payload: response,
      });
      if (callback) callback(response);
    }, 
    
    //历史密钥
    *queryHistoryList({ payload }, { call, put }) {
      const response = yield call(queryHistoryKey, payload);
      yield put({
        type: 'saveHistoryData',
        payload: response,
      });
    },

    
    //密钥规则列表
    *selectKeyRule({ payload, callback }, { call, put }) {
      const response = yield call(querySelectKeyRule, payload);
      yield put({
        type: 'keyRule',
        payload: response,
      });
      if (callback) callback(response);
    },

    //应用系统列表
    *selectAppList({ payload, callback }, { call, put }) {
      const response = yield call(querySelectApp, payload);
      yield put({
        type: 'saveApp',
        payload: response,
      });
      if (callback) callback(response);
    },

    //场景列表
    *selectSceneList({ payload, callback }, { call, put }) {
      const response = yield call(querySelectScene, payload);
      yield put({
        type: 'saveScene',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data || [],
      };
    },

    saveHistoryData(state, action) {
      return {
        ...state,
        historyData: action.payload.data || [],
      };
    },

//密钥规则
keyRule(state, action) {
  return {
    ...state,
    keyRuleData: action.payload.data,
  }; 
},

//应用系统
saveApp(state, action) {
   return {
    ...state,
    appData: action.payload.data,
  };
},

//场景
saveScene(state, action) {
  return {
    ...state,
    sceneData: action.payload.data,
  }; 
},

  },
};
